﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using CQTool;

namespace CQToolRegistrar
{
    /// <summary>
    /// http://mnaoumov.wordpress.com/2012/09/26/developing-custom-tool-aka-single-file-generators-for-visual-studio-2012/
    /// </summary>
    internal class Program
    {
        private static void Main()
        {
            {
                var process = new Process {
                    StartInfo = {
                        UseShellExecute = false,
                        FileName = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "regasm.exe"),
                        Arguments = @" /codebase """ + typeof(CQToolGenerator).Assembly.Location + @""""
                    }
                };
                process.Start();
                process.WaitForExit();
            }
            {
                const string path = "Install.reg";
                File.WriteAllText(path, @"Windows Registry Editor Version 5.00
  
; TODO: Replace with your GUID
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\12.0_Config\CLSID\{" + CQToolGenerator.Guid + @"}]
""InprocServer32""=""C:\\Windows\\System32\\mscoree.dll""
""ThreadingModel""=""Both""
; TODO: Replace with your class
""Class""=""" + typeof (CQToolGenerator).FullName + @"""
; TODO: Replace with your assembly
""Assembly""=""" + typeof (CQToolGenerator).Assembly.FullName + @"""
; TODO: Replace with your dll file
""CodeBase""=""file:///" + typeof (CQToolGenerator).Assembly.Location.Replace(@"\", @"\\") + @"""
 
; TODO: Replace with your tool name
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0_Config\Generators\{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}\" + CQToolGenerator.Name + @"]
; TODO: Replace with your GUID
""CLSID""=""{" + CQToolGenerator.Guid + @"}""
""GeneratesDesignTimeSource""=dword:00000001
; TODO: Replace with your description
@=""" + CQToolGenerator.Name + @"""");
                var process = new Process {
                    StartInfo = {
                        UseShellExecute = false,
                        FileName = "regedit.exe",
                        Arguments = "/s " + Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(),
                            Path.Combine(Path.GetDirectoryName(typeof (Program).Assembly.Location), path))
                    }
                };
                process.Start();
                process.WaitForExit();
            }
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}